<?php
require_once PATH_NAME .'/models/users/users.php';
require_once PATH_NAME .'/utilities/sessions.php';
require_once PATH_NAME .'/views/users/users.php';
require_once PATH_NAME .'/includes/access.php';

class usersController{
	public function __construct(){}
	
	public function loginForm(){
		$login = new userView();
		$login->loginForm();
	}
	
	public function login( $data ){
		$userModel = new usersModel(); 
		$username = $data['username'];
		$password = $data['password'];
		$result = $userModel->login($username, $password);
		$result = $result[0];
		if ( !empty($result) ){
			$session = new sessions();
			$storesesion['user_id'] = $result->id;
			$storesesion['name'] = $result->name;
			$session->login_session($storesesion);
		}else{
			header("Location: index.php");exit;
		}
		header("Location: index.php");exit;
	}
	public function logout(){
		$session = new sessions();
		$session->clear_session();
		header("Location: index.php");exit;
	}
	
	public function showAgentList(){
		$user = sessions::getSession();
		$show = new userView();
		$userModel = new usersModel();
		$showlists = $userModel->showAgentList( $user['user_id'] );		
		$show->showAgentList( $showlists );
		exit;
	}
	public function showCustomersList(){
		$user = sessions::getSession();
		$show = new userView();
		$userModel = new usersModel();
		$showlists = $userModel->showCustomerList( $user['user_id'] );
		$show->showCustomersList( $showlists );
	}
	public function regCustomerForm(){
		$registration = new userView();
		$registration->regCustomerForm();
	}
		
	public function showContractList(){
		$user = sessions::getSession();
		$show = new userView();
		$userModel = new usersModel();
		$rowsPerPage = 10;
		$offset = ($pageNum - 1) * $rowsPerPage;
		$limits = " limit $offset, $maxRow order id ";
		$showlists = $userModel->showContractList( $user['user_id'], $limits );
		$show->showContractList( $showlists );
	}
	public function regContractForm(){
		$registration = new userView();
		$registration->regContractForm();
	}
	public function editCustomerList( $id = 0 ){
		$edit = new userView();	
		$userModel = new usersModel();
		$lists = $userModel->getUserById( $id );
		$data['info']['id'] = $lists->id;
		$data['info']['fullname'] = $lists->name;
		$data['info']['sex'] = $lists->gendor;
		$data['info']['tel'] = $lists->phone_number;
		$data['info']['address'] = $lists->address;
		$data['info']['username'] = $lists->username;
		$data['info'][0]['username'] = "disabled='disabled'";
		list($data['info']['yeardob'], $data['info']['monthdob'], $data['info']['daydob']) = split('-', $lists->birthdate);
		$edit->regCustomerForm( $data );
	}
	public function saveRegCustomer( $data ){
		$usersModel = new usersModel();		
		$id = $data['id'];
		$data['sql'][0]['fullname'] = 'name';
		$data['sql'][0]['sex'] = 'gendor';
		$data['sql'][0]['birthdate'] = 'birthdate';
		//$data['sql'][0]['email'] = 'name';
		$data['sql'][0]['tel'] = 'phone_number';
		$data['sql'][0]['address'] = 'address';				
		$data['sql'][0]['create'] = 'create';
		$data['sql'][0]['publish'] = 'publish';
		$data['birthdate'] = $data['yeardob']."-".$data['monthdob']."-".$data['daydob'];		
		$data['create'] = date('Y-m-d');
		$data['publish'] = 1;
		if ( !empty($data['id']) and $data['id'] > 0){
			$data['sql'][0]['id'] = 'id';
			if ( !empty($data['passwords']) ){				
				$data['sql'][0]['passwords'] = 'password';
				$data['passwords'] = md5($data['password']);			
			}			
			$usersModel->editCustomer($data);
			$url = 'users.php?edit=customer&id='.$id;
		}else{
			$data['sql'][0]['username'] = 'username';
			$data['sql'][0]['passwords'] = 'password';
			$data['passwords'] = md5($data['password']);		
			$id = $usersModel->saveRegCustomer($data);
			$url = 'users.php?edit=customer&id='.$id;
		}		
		Access::setRedirect($url);
	}
	public function saveRegContract( $data ){
		$registration = new usersModel();
		$data['sql'][0]['conName'] = 'con_name';
		$data['sql'][0]['conStartDate'] = 'con_datestart';
		$data['sql'][0]['conEndDate'] = 'con_dateend';		
		$data['sql'][0]['description'] = 'description';		
		$data['sql'][0]['create'] = 'create';			
		$data['sql'][0]['publish'] = 'publish';
		$data['create'] = date('Y-m-d');
		$data['publish'] = 1;
		$registration->saveRegContract($data);
		$url = 'index.php';
		Access::setRedirect($url);
	}
}
?>